#include <iostream>
#include <string.h>

using namespace std;

//定义类Person，包含姓名（char*）、年龄，
//提供成员函数 display输出信息。
//类Employee由Person派生，包含部门（char*）、工资,
//成员函数display用来输出职工姓名、年龄、部门、工资。
//要求Employee对象能够进行正确的复制、赋值操作，
//同时提供计算平均工资的功能。

class Person{
public:
    // 构造函数
    Person(const char *name, int age)
    :_name(new char[strlen(name) + 1]())
    ,_age(age)
    {
        strcpy(_name, name);
    }
    
    // 析构函数
    ~Person(){
        if(_name){
            delete [] _name;
            _name = nullptr;
        }
    }

    // 拷贝构造函数
    Person(const Person & rhs)
    :_name(new char[strlen(rhs._name) + 1]())
    ,_age(rhs._age)
    {   
        strcpy(_name,rhs._name);
    }

    // 赋值运算符
    Person & operator=(const Person & rhs){
        if(this != &rhs){
            delete [] _name;
            _name = new char[strlen(rhs._name) + 1]();
            strcpy(_name,rhs._name);
            _age = rhs._age;
        }
        return *this;
    }

    void display(){
        cout << "_name=" << _name << " _age=" << _name << endl;
    }

private:
    char * _name;
    int _age;
};

class Employee : public Person{
public:

    Employee(const char * name, int age, const char * depart, double salary)
    :Person(name, age)
    ,_depart(new char[strlen(depart) + 1]())
    ,_salary(salary)
    {
        strcpy(_depart,depart);
        _totalsalary += _salary;
        ++_totalnum;
    }


    void display(){
        Person::display();
        cout << "_depart=" << _depart << " _salary=" << _salary << endl;
    }

    // 拷贝构造函数
    Employee(const Employee & rhs)
    :Person(rhs)
    ,_depart(new char[strlen(rhs._depart) + 1]())
    ,_salary(rhs._salary)
    {
        strcpy(_depart, rhs._depart);
        ++_totalsalary += rhs._salary;
        ++_totalnum;
    }

    // 赋值运算符
    Employee & operator=(const Employee & rhs){
        if(this != &rhs){
            // 赋值也要把这个基类赋值过去
            // 需要记住这个重要的复制
            Person::operator=(rhs);
            delete [] _depart;
            _depart = new char[strlen(rhs._depart) + 1]();
            _totalsalary -= _salary;
            strcpy(_depart,rhs._depart);
            _salary = rhs._salary;
            _totalsalary += _salary;
        }
        return *this;
    }
    // 析构函数
    ~Employee(){
        if(_depart){
            delete [] _depart;
            _depart = nullptr;
        }
    }
    // 计算此时的人均工资
    static double getArray(){
        return _totalsalary / _totalnum;
    }

private:
    char * _depart;
    double _salary;
    // 设置静态数据 存储工资总数 和 公司总人数
    static double _totalsalary;
    static int _totalnum;
};
double Employee::_totalsalary = 0;
int Employee::_totalnum = 0;

void test(){
    Employee e1("ll", 23, "研发部", 15555);
    e1.display();
    cout << Employee::getArray() << endl;
    Employee e2("lsls", 23, "研发部", 18888);
    e2.display();
    cout << Employee::getArray() << endl;
    Employee e3("kk", 23, "研发部", 15999);
    e3.display();
    cout << Employee::getArray() << endl;
    Employee e4("zzz", 23, "研发部", 15888);
    e4.display();
    cout << Employee::getArray() << endl;
    Employee e5 = e1;
    e5.display();
    cout << Employee::getArray() << endl;
    e3 = e4;
    e3.display();
    cout << Employee::getArray() << endl;


}

int main(int atgc, char * argv[])
{
    test();
    return 0;
}

